package book.view;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import book.controller.BookControllerListener;
import book.po.Book;

/**
 * 添加/修改图书对话框
 * @author Dengcj
 *
 */
public class BookDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private BookControllerListener bookControllerListener;
	private JTextField isbnTextField,nameTextField,priceTextField;
	private JButton confirmButton;
	private JPanel bookAddPanel;
	private Book book;
	public BookDialog(BookFrame owner,String title,Book book) {
		this.bookControllerListener = owner.getControllerListener();  //让BookFrame和BookAddDialog共用同一个controller对象
		this.bookControllerListener.setBookAddDialog(this);
		this.setTitle(title);
		this.book = book;
		this.add(getBookAddPanel());
		this.setModal(true); //设有模
		this.setSize(230, 160);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	/**
	 * 添加/修改图书表单面板
	 * @return
	 */
	private JPanel getBookAddPanel() {
		bookAddPanel = new JPanel();
		if("添加图书".equals(this.getTitle())) {
			isbnTextField = new JTextField(12);
			nameTextField = new JTextField(12);
			priceTextField = new JTextField(12);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addPost"); //添加图书
		}else {
			isbnTextField = new JTextField(String.valueOf(book.getIsbn()),12);
			nameTextField = new JTextField(book.getName(),12);
			priceTextField = new JTextField(String.valueOf(book.getPrice()),12);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updatePost"); //修改图书
		}
		confirmButton.addActionListener(bookControllerListener);
		bookAddPanel.add(new JLabel("书籍ISBN"));
		bookAddPanel.add(isbnTextField);
		bookAddPanel.add(new JLabel("书籍名称"));
		bookAddPanel.add(nameTextField);
		bookAddPanel.add(new JLabel("书籍单价"));
		bookAddPanel.add(priceTextField);
		bookAddPanel.add(confirmButton);
		return bookAddPanel;
	}
	public JTextField getIsbnTextField() {
		return isbnTextField;
	}
	public JTextField getNameTextField() {
		return nameTextField;
	}
	public JTextField getPriceTextField() {
		return priceTextField;
	}
	public JButton getConfirmButton() {
		return confirmButton;
	}
}
